package com.example.demo.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 服务器返回前台统一处理
 */
@ApiModel(value = "Result", description = "restful接口统一回包数据格式")
public class Result implements Serializable {

    private static final long serialVersionUID = -3948389268046368059L;

    //业务状态码
    @ApiModelProperty(value = "业务状态码", name = "code")
    private Integer code;

    //提示信息
    @ApiModelProperty(value = "提示信息", name = "msg")
    private String msg;

    //调用成功后是否返回前台json
    @ApiModelProperty(value = "成功后返回所需json信息", name = "data")
    private Object data;

    public Result() {}

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 调用接口成功后返回只包含状态码的信息
     * @return
     */
    public static Result success() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 调用接口成功后返回只包含状态码的信息
     * @param msg
     * @return
     */
    public static Result success(String msg) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setMsg(msg);
        return result;
    }

    /**
     * 如果是查询，调用成功后返回包含json数据的信息。json放入data
     * @param data
     * @return
     */
    public static Result success(Object data) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 调用失败后返回只包含状态码的信息
     * @param resultCode
     * @return
     */
    public static Result failure(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    public static Result failure(Integer code,String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 调用失败后返回包含状态码和msg
     * @param resultCode
     * @param msg
     * @return
     */
    public static Result failure(ResultCode resultCode, String msg) {
        Result result = new Result();
        result.setResultCode(resultCode);
        result.setMsg(msg);
        return result;
    }

    /**
     * 调用失败后返回包含状态码和msg
     * @param msg
     * @return
     */
    public static Result failure(String msg) {
        Result result = new Result();
        result.setResultCode(ResultCode.ERROR);
        result.setMsg(msg);
        return result;
    }

    /**
     * 调用失败后返回包含状态码和data
     * @param resultCode
     * @param data
     * @return
     */
    public static Result failure(ResultCode resultCode, Object data) {
        Result result = new Result();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
